import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class OneDimensionalBalls {
	public long countValidGuesses(int[] firstPicture, int[] secondPicture) {
		Arrays.sort(firstPicture);
		Set<Integer> possibleTimes = new HashSet<Integer>();
		for (int aSecondPicture : secondPicture)
			possibleTimes.add(Math.abs(aSecondPicture - firstPicture[0]));
		long validGuesses = 0;
		for (int time : possibleTimes) {
			if (time == 0)
				continue;
			int[] left = new int[firstPicture.length];
			int[] right = new int[firstPicture.length];
			Arrays.fill(left, -1);
			Arrays.fill(right, -1);
			for (int i = 0; i < firstPicture.length; i++) {
				for (int j = 0; j < secondPicture.length; j++) {
					if (firstPicture[i] - time == secondPicture[j])
						left[i] = j;
					if (firstPicture[i] + time == secondPicture[j])
						right[i] = j;
				}
			}
			boolean isValid = true;
			for (int i = 0; i < firstPicture.length && isValid; i++) {
				if (left[i] == -1 && right[i] == -1)
					isValid = false;
				if (left[i] == -1) {
					for (int j = i + 1; j < firstPicture.length; j++) {
						if (right[i] == left[j])
							left[j] = -1;
					}
				}
				if (right[i] == -1) {
					int current = left[i];
					for (int j = i - 1; j >= 0; j--) {
						if (right[j] == current) {
							right[j] = -1;
							current = left[j];
						}
					}
				}
			}
			if (!isValid)
				continue;
			int[] fIndex = new int[firstPicture.length];
			long currentGuesses = 1;
			for (int i = 0; i < firstPicture.length; i++) {
				if (left[i] == -1 || right[i] == -1)
					continue;
				boolean found = false;
				for (int j = i + 1; !found && j < firstPicture.length; j++) {
					if (left[j] == right[i]) {
						fIndex[j] = fIndex[i] + 1;
						found = true;
					}
				}
				if (!found)
					currentGuesses *= fIndex[i] + 2;
			}
			validGuesses += currentGuesses;
		}
		return validGuesses;
	}

// BEGIN CUT HERE
   public static void main(String[] args) {
		if ( args.length == 0 ) {
			OneDimensionalBallsHarness.run_test(-1);
		} else {
			for ( int i=0; i<args.length; ++i )
				OneDimensionalBallsHarness.run_test(Integer.valueOf(args[i]));
		}
	}
// END CUT HERE
}

// BEGIN CUT HERE
class OneDimensionalBallsHarness {
	public static void run_test( int casenum ) {
		if ( casenum != -1 ) {
			if ( runTestCase( casenum ) == -1 )
				System.err.println("Illegal input! Test case " + casenum + " does not exist.");
			return;
		}
		
		int correct = 0, total = 0;
		for ( int i=0;; ++i ) {
			int x = runTestCase(i);
			if ( x == -1 ) {
				if ( i >= 100 ) break;
				continue;
			}
			correct += x;
			++total;
		}
		
		if ( total == 0 ) {
			System.err.println("No test cases run.");
		} else if ( correct < total ) {
			System.err.println("Some cases FAILED (passed " + correct + " of " + total + ").");
		} else {
			System.err.println("All " + total + " tests passed!");
		}
	}
	
	static boolean compareOutput(long expected, long result) { return expected == result; }
	static String formatResult(long res) {
		return String.format("%d", res);
	}
	
	static int verifyCase( int casenum, long expected, long received ) { 
		System.err.print("Example " + casenum + "... ");
		if ( compareOutput( expected, received ) ) {
			System.err.println("PASSED");
			return 1;
		} else {
			System.err.println("FAILED");
			System.err.println("    Expected: " + formatResult(expected)); 
			System.err.println("    Received: " + formatResult(received)); 
			return 0;
		}
	}

	static int runTestCase( int casenum ) {
		switch( casenum ) {
		case 0: {
			int[] firstPicture        = {12,11};
			int[] secondPicture       = {10,11,13};
			long expected__           = 3L;

			return verifyCase( casenum, expected__, new OneDimensionalBalls().countValidGuesses( firstPicture, secondPicture ) );
		}
		case 1: {
			int[] firstPicture        = {1,2,3};
			int[] secondPicture       = {1,2,3};
			long expected__           = 0L;

			return verifyCase( casenum, expected__, new OneDimensionalBalls().countValidGuesses( firstPicture, secondPicture ) );
		}
		case 2: {
			int[] firstPicture        = {1,3};
			int[] secondPicture       = {1,3};
			long expected__           = 1L;

			return verifyCase( casenum, expected__, new OneDimensionalBalls().countValidGuesses( firstPicture, secondPicture ) );
		}
		case 3: {
			int[] firstPicture        = {7234};
			int[] secondPicture       = {6316,689156,689160,689161,800000,1000001};
			long expected__           = 6L;

			return verifyCase( casenum, expected__, new OneDimensionalBalls().countValidGuesses( firstPicture, secondPicture ) );
		}
		case 4: {
			int[] firstPicture        = {6,2,4};
			int[] secondPicture       = {1,2,3,4,5,7,8};
			long expected__           = 7L;

			return verifyCase( casenum, expected__, new OneDimensionalBalls().countValidGuesses( firstPicture, secondPicture ) );
		}

		// custom cases

/*      case 5: {
			int[] firstPicture        = ;
			int[] secondPicture       = ;
			long expected__           = L;

			return verifyCase( casenum, expected__, new OneDimensionalBalls().countValidGuesses( firstPicture, secondPicture ) );
		}*/
/*      case 6: {
			int[] firstPicture        = ;
			int[] secondPicture       = ;
			long expected__           = L;

			return verifyCase( casenum, expected__, new OneDimensionalBalls().countValidGuesses( firstPicture, secondPicture ) );
		}*/
/*      case 7: {
			int[] firstPicture        = ;
			int[] secondPicture       = ;
			long expected__           = L;

			return verifyCase( casenum, expected__, new OneDimensionalBalls().countValidGuesses( firstPicture, secondPicture ) );
		}*/
		default:
			return -1;
		}
	}
}

// END CUT HERE
